package com.hub.eshop.web;


import com.hub.eshop.entity.NewBeeMallGoods;
import com.hub.eshop.service.CacheService;
import com.hub.eshop.service.NewshopGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.websocket.SendResult;

@Controller
public class GoodsController {

    @Autowired
    private NewshopGoodsService newshopGoodsService;

    @Autowired
    private CacheService cacheService;



    @GetMapping("randomGood")
    @ResponseBody
    public NewBeeMallGoods send(@RequestParam String idStr) {

        NewBeeMallGoods productInfo = null;

        try {
            Long goodsId = Long.parseLong(idStr);

            // 从Redis中取出数据
            productInfo = cacheService.getProductInfoFromReidsCache(goodsId);

            if(productInfo != null){
                System.out.println("从redis获取到数据");
                return productInfo;
            }

            // 从本地缓存取
            productInfo = cacheService.getProductInfoFromLocalCache(goodsId);
            if(productInfo !=null ){
                cacheService.saveProductInfo2ReidsCache(productInfo);
                System.out.println("从本地缓存获取到数据");
                return productInfo;
            }

            // 从数据库取
            productInfo = newshopGoodsService.getNewBeeMallGoodsById(10003L);
            if(productInfo !=null ){
                cacheService.saveProductInfo2LocalCache(productInfo);
                System.out.println("从数据库获取数据");
                return productInfo;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return productInfo;
    }
}
